package main

import "fmt"

func removeDuplicates(nums []int) int {
	if len(nums) <= 1 {
		return len(nums)
	}
	// 双指针, i记录保存元素位置 j记录待检查元素

	i, j := 0, 1
	cnt := 1
	for i < j && j < len(nums)-1 {
		// j=i && j=j+1 => j++
		if nums[j] == nums[j+1] && nums[j] == nums[i] {
			j++
			continue
		}
		nums[i+1] = nums[j]
		cnt++
		i++
		j++
	}
	return cnt
}

func main() {
	nums := []int{1, 1, 1, 2, 2, 3}
	fmt.Println(nums, removeDuplicates(nums))
}
